
import numpy as np

def outliers_sigma_indices(array, factor):
    indices = []
    left=array.mean()-factor*array.std()
    right=array.mean()+factor*array.std()
    for i in range(len(array)):
        if array[i]<= left or array[i] >= right:
            indices.append(i)
    return indices



def remove_outliers_sigma(array, factor):
    left=array.mean()-factor*array.std()
    right=array.mean()+factor*array.std()
    return array[(left<=array)&(array<=right)]

if __name__ == "__main__":
    #用numpy随机生成100个服从正态分布的随机数
    num=np.random.randn(100)
    #随机插入两个异常值进去，此时num.shape[0]==102
    np.append(num,10)
    print(num.shape[0])
    print("num:{}".format(num))

    #设定法则的左右边界
    left=num.mean()-3*num.std()
    right=num.mean()+3*num.std()

    #获取在范围内的数据
    new_num=num[(left<num)&(num<right)]
    # new_num.shape
    print("new_num:{}".format(new_num))
    #结果为100，已经剔除了刚开始插入的两个异常值

    # ————————————————
    # 版权声明：本文为CSDN博主「晴天的晴诶」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
    # 原文链接：https://blog.csdn.net/d_vv_b/article/details/104978361